// pages/forget/forget.ts
const app = getApp()  // 获取App实例
Page({

  /**
   * 页面的初始数据
   */
  data: {
    daojishi: 0,
    phone: "",
    password: "",
    code: "",
    logo: app.getImgUrl('logo.png'),
  },
  gologin() {
    wx.reLaunch({
      url: '/pages/login/login'
    })
  },
  showerror(content) {
    wx.showToast({
      title: content,
      icon: 'error',
      duration: 1500
    })
  },
  async changepass() {
    if (!this.data.ischack) {
      this.showerror('请阅读并同意用户协议')
      return
    }
    if (!this.data.phone || this.data.phone.length < 4) {
      wx.showToast({
        title: '手机格式不正确',
        icon: 'error',
        duration: 1500
      })
      return
    }
    if (!this.data.code) {
      this.showerror('请输入验证码')
      return
    }
    // 判断两次密码是否相同
    if (this.data.yespassword != this.data.password) {
      this.showerror('两次密码不一样')
      return
    }
    let res = await app.axios(
      'user',
      {
        op: "ajaxUpUserBindPhoneReset",
        code: this.data.code,
        password: this.data.password,
        phone: this.data.phone,

      },
      false
    )
    wx.showToast({
      title: '修改成功',
      icon: 'success',
      duration: 1500,
      success: () => {
        setTimeout(() => {
          wx.reLaunch({
            url: '/pages/login/login'
          })
        }, 1500);
      }
    })
  },

  changechack() {
    this.setData({
      ischack: !this.data.ischack
    })
  },
  //获取验证码按钮
  getyanzheng() {
    if (!this.data.phone) {
      this.showerror('请输入管理账号')
      return
    }
    if (this.data.daojishi > 0) {
      wx.showToast({
        title: '请勿过于频繁获取验证码',
        icon: 'error',
        duration: 1500
      })
      return
    }
    app.axios(
      'user',
      {
        op: "ajaxSendCode",
        "phone": this.data.phone,
        password: this.data.password,
      },
      false
    )
      .then(res => {
        this.setData({
          daojishi: 30
        })

        this.setData({
          time: setInterval(() => {

            this.setData({
              daojishi: this.data.daojishi - 1
            })
            if (this.data.daojishi <= 0) {
              this.setData({
                daojishi: 0
              })
              if (this.data.time) {
                clearInterval(this.data.time)
              }
            }
            console.log(this.data.daojishi)
          }, 1000)
        })
        wx.showToast({
          title: '验证码发送成功',
          icon: 'success',
          duration: 1500
        })
      });

  },
})